package com.asset.dispoal.business.repayment.service.strategy;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCaseMediation;
import com.asset.dispoal.business.repayment.domain.db.mapper.RepaymentMapper;
import com.asset.dispoal.business.repayment.dto.RepaymentInfoResponse;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 调解状态，按照期数还，从未还清的期数开始加载
 * @author: yf
 * @date: 2022-09-28 16:40:33
 */
@Component("mediationState")
public class MeditionCase implements IRepaymentStrategy {
    @Autowired
    private RepaymentMapper repaymentMapper;

    @Override
    public RepaymentInfoResponse getRepaymentInfo(BsCase bsCase) {
        List<BsCaseMediation> bsCaseMediations = repaymentMapper.getBsCaseMeditionByCaseId(bsCase.getId());
        //获取第一条未还清的记录
        BsCaseMediation caseMediation = bsCaseMediations.get(0);
        AgreementCaseSateMenu caseSateMenu = AgreementCaseSateMenu.getByLabel(bsCase.getCaseState());

        //已还本金 利息 罚息
        BigDecimal repaymentPrincipal = caseMediation.getRepaymentPrincipal() == null ? BigDecimal.ZERO : caseMediation.getRepaymentPrincipal();
        BigDecimal repaymentInterest = caseMediation.getRepaymentInterest() == null ? BigDecimal.ZERO : caseMediation.getRepaymentInterest();
        BigDecimal repaymentPenalty = caseMediation.getRepaymentPenalty() == null ? BigDecimal.ZERO : caseMediation.getRepaymentPenalty();
        //待还 本金 利息 罚息
        BigDecimal toDoReturnPrincipalAmount = caseMediation.getPrepaymentPrincipal().subtract(repaymentPrincipal);
        BigDecimal toDoInterestAmount = caseMediation.getPrepaymentInterest().subtract(repaymentInterest);
        BigDecimal toDoLitigationFines = caseMediation.getPrepaymentPenalty().subtract(repaymentPenalty);


        return RepaymentInfoResponse.builder()
                .repaymentId(caseMediation.getId())
                .caseId(bsCase.getId())
                .caseState(bsCase.getCaseState())
                .caseStateName(caseSateMenu.getValue())
                .lawsuitPrincipalAmount(caseMediation.getPrepaymentPrincipal().toPlainString())
                .lawsuitInterestAmount(caseMediation.getPrepaymentInterest().toPlainString())
                .litigationFines(caseMediation.getPrepaymentPenalty().toPlainString())
                .toDoReturnPrincipalAmount(toDoReturnPrincipalAmount.toPlainString())
                .toDoInterestAmount(toDoInterestAmount.toPlainString())
                .toDoLitigationFines(toDoLitigationFines.toPlainString())
                .repaymentPrincipal(repaymentPrincipal.toPlainString())
                .repaymentInterest(repaymentInterest.toPlainString())
                .repaymentPenalty(repaymentPenalty.toPlainString())
                .period(caseMediation.getPeriod().toPlainString())
                .isPay(caseMediation.getIsPay())
                .build();
    }
}
